package com.naodong.shenluntiku.mvp.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.mvp.model.bean.Assessment;
import com.naodong.shenluntiku.mvp.model.bean.EventBusTag;
import com.naodong.shenluntiku.mvp.model.bean.SubjectInfo;
import com.naodong.shenluntiku.mvp.view.widget.CircleTextView;
import com.naodong.shenluntiku.mvp.view.widget.PointView;
import com.squareup.phrase.Phrase;
import com.yatatsu.autobundle.AutoBundleField;

import butterknife.BindView;
import butterknife.OnClick;
import me.shingohu.man.base.BaseMVPFragment;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.EventManager;
import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/5/24.
 */

public class AssessmentResultFragment extends BaseMVPFragment {

    @BindView(R.id.flexbox)
    FlexboxLayout flexboxLayout;
    @AutoBundleField(required = false)
    Assessment assessment;
    LayoutInflater layoutInflater;


    @BindView(R.id.totalFinishTV)
    TextView totalFinishTV;

    @BindView(R.id.rightPointTV)
    ImageView rightPointIV;
    @BindView(R.id.leftPointTV)
    ImageView leftPointIV;
    @BindView(R.id.thirdPointTV)
    ImageView thirdPointIV;


    @BindView(R.id.pointView)
    PointView pointView;




    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    public static AssessmentResultFragment getInstance(Assessment assessment) {
        AssessmentResultFragment frgment = new AssessmentResultFragment();
        frgment.setAssessment(assessment);

        return frgment;
    }


    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        layoutInflater = LayoutInflater.from(mActivity);
        setAnswerSheetView();
    }

    public void setAssessment(Assessment assessment) {
        this.assessment = assessment;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();

    }


    private void setAnswerSheetView() {
        flexboxLayout.removeAllViews();

        int point = assessment.getAssessCorrectNum() * 100 / assessment.getAssessNum();

        if (point < 10) {
            rightPointIV.setImageResource(UiUtils.getDrawableIdByName("vector_drawable_ico_num_" + point));
        } else if (point == 100) {
            leftPointIV.setImageResource(R.drawable.vector_drawable_ico_num_1);
            rightPointIV.setImageResource(R.drawable.vector_drawable_ico_num_0);
            thirdPointIV.setImageResource(R.drawable.vector_drawable_ico_num_0);
        } else {
            leftPointIV.setImageResource(UiUtils.getDrawableIdByName("vector_drawable_ico_num_" + point / 10));
            rightPointIV.setImageResource(UiUtils.getDrawableIdByName("vector_drawable_ico_num_" + point % 10));
        }

        pointView.setProgress(point);
        totalFinishTV.setText(Phrase.from("共作答: {count}道题").put("count", assessment.getAssessNum()).format());




        int width = (UiUtils.getScreenWidth() - UiUtils.dip2px(10) * 2) / 5;
        int height = UiUtils.dip2px(60);

        for (int i = 0; i < assessment.getAssessNum(); i++) {
            SubjectInfo subjectInfo = assessment.getSubjects().get(i);
            View view = layoutInflater.inflate(R.layout.answersheet_option, null);
            CircleTextView circleTextView = (CircleTextView) view.findViewById(R.id.opTV);
            circleTextView.setText((i + 1) + "");
            FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(width, height);
            view.setLayoutParams(lp);


            if (subjectInfo.getuIsCorrect() == 1) {
                //回答正确
                circleTextView.setBgColor(R.color.color72CEB7);
            } else {
                if (subjectInfo.getuChooseIds().size() > 0) {
                    //回答错误
                    circleTextView.setBgColor(R.color.colorAccent);
                } else {
                    //未作答
                    circleTextView.setBgColor(R.color.color929292);
                }
            }
            int finalI = i;
            view.setOnClickListener(v -> EventManager.getInstance().postIntEvent(EventBusTag.TO_ASSIGN_SUBJECT, finalI));
            flexboxLayout.addView(view);
        }

    }


    @OnClick(R.id.submitBtn)
    public void submitClick() {
        EventManager.getInstance().postEvent(EventBusTag.CHECK_ASSESSMENT_RESULT);
    }


    @Override
    protected int layoutId() {
        return R.layout.f_assessment_result;
    }
}
